/* 
 * %W% %E% Enrique Madrid
 *
 * Copyright (c) 1975-2008 M&T Consulting, Inc. All rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
*/

package com.myt.util.att;

import org.apache.commons.cli.*;

import java.io.*;
import java.util.Arrays;

/**
 * Describe class <code>AttApp</code> here.
 *
 * @author <a href="mailto:emadridm@gmail.com">Enrique Madrid</a>
 * @version 1.0
 */
public class AttApp {

    public final static int ATP_OPT_REQUIRED = 0x0001;
    public final static int ATP_ARG_REQUIRED = 0x0002;

    protected String name = "AttApp";

    private Options options = new Options();

    private CommandLine command = null;

    public AttApp() {
	// Default comamand line options
	acceptOption("h", "Show this usage display");
    }

    public AttApp(String name) {
	setName(name);
    }

    public void setName(String n) {
	name = n;
    }

    public String getName() {
	return name;
    }

    public void acceptOption(String option, String optDescription, 
			     int flags, String argName) {

	try {

	    Option opt = new Option(option, optDescription);

	    if ((flags & ATP_OPT_REQUIRED) > 0) {
		opt.setRequired(true);
	    }

	    if ((flags & ATP_ARG_REQUIRED) > 0) {
		opt.setOptionalArg(true);
		opt.setArgs(1);
	    }

	    if (argName != null) {
		opt.setArgName(argName);
	    }

	    options.addOption(opt);

	} catch (Exception e) {
	    // I don't care!
	}
	
    }

    protected String[] buildArgs(String[] args, int from, int to) {
	String[] ret = null;
	if (args == null) {
	    ret = new String[0];
	} else {
	    from = (from > -1 && from < args.length) ? from : 0;
	    to = (to > -1 && to <= args.length) ? to : args.length;
	    ret = (to - from <= 0) ? new String[0] : Arrays.copyOfRange(args, from, to);
	}
	return ret;
    }

    public void acceptOption(String option, String optDescription) {
	acceptOption(option, optDescription, 0, null);
    }

    public void acceptOption(String option) {
	acceptOption(option, null, 0, null);
    }

    public boolean parseOptions(String[] args) {
	boolean ret = false;

	try {
	    Parser getopt = new BasicParser();
	    command = getopt.parse(options, args, true);
	    ret = setOptions();
	} catch (ParseException pe) {
	    command = null;
	    ret = false;
	} catch (Exception e) {
	    ret = true;
	}

	return ret;
    }

    protected boolean setOptions() {
	return true;
    }

    public void printHelp() {
	HelpFormatter helper = new HelpFormatter();
	helper.printOptions(new PrintWriter(System.out, true), 
			    helper.getWidth(), 
			    options,
			    helper.getLeftPadding(), 
			    helper.getDescPadding());
    }

    public void printHelp(String syntaxis) {
	HelpFormatter helper = new HelpFormatter();
	helper.printHelp(syntaxis, options, true); 
    }

    public boolean hasOption(String option) {
 	return ((command != null) ? command.hasOption(option) : false);
    }

    public void exec(String[] args) {
	printHelp();
    }

    protected String argumentOf(String option) {
	String ret = "";
	if (command != null) {
	    ret = command.getOptionValue(option);
	}
	return ret;
    }
}